﻿
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using PiCamera;
using PiControls.Controls.DriveUI;
using PiControls.Controls.DriveUI.CameraUI;
using PiControls.Controls.DriveUI.ModbusUI;
using PiControls.Controls.DriveUI.TCPClient;
using PiControls.Controls.myTabControl;
using PiControls.Controls.Switch;
using PiFramework;
using PiUi.View;
namespace PiUi.Froms
{
    public partial class SettingView : BaseView
    {
        CustomTabControl tabControl = null;
        Dictionary<string, TabPage> tabPages = new Dictionary<string, TabPage>();
        bool flag;
        AutoSizeFormClass asc = new AutoSizeFormClass();
        public SettingView()
        {
            InitializeComponent();
            tabControl = new CustomTabControl();
            this.SizeChanged += my_SizeChanged;
        }
        public void addCamera(CameraInfoConfig cameraInfoConfig) {
        
            string addobjectName = cameraInfoConfig.ObjectName;
            //PTcpClientControl.Instance.loadTcpClient(tcpClientInfoConfig);
            //bool isConnect = PCameraControlHelp.Instance.GetCamera(cameraInfoConfig.ObjectName).IsRunConnect;
            CameraControl configCameraUi = new CameraControl(
                cameraInfoConfig.ObjectName,
                cameraInfoConfig.CameraClass,
                cameraInfoConfig.exposure);
            configCameraUi.Dock = DockStyle.Fill;
            TabPage tabPage1 = new TabPage(cameraInfoConfig.ObjectName);
            tabPage1.Font = new System.Drawing.Font("微软雅黑", 11, System.Drawing.FontStyle.Bold);
            tabPage1.Controls.Add(configCameraUi);
            tabControl.TabPages.Add(tabPage1);
            tabPages.Add(cameraInfoConfig.ObjectName, tabPage1);
                    


        }
        public override void LoadTheme()
        {

            tabPages.Clear();
            tabControl.TabPages.Clear();
            List<TcpClientInfoConfig> tcpClientInfoConfigList = PTcpClientControl.Instance.getTcpClientInfoConfigList();
            foreach (TcpClientInfoConfig tcpClientInfoConfig in tcpClientInfoConfigList)
            {
                addTcpClien(tcpClientInfoConfig);
            }
            List<CameraInfoConfig> cameraInfoConfigs = PCameraControlHelp.Instance.getCameraList();
            foreach (CameraInfoConfig cameraInfoConfig in cameraInfoConfigs) {
                addCamera(cameraInfoConfig);
            }

            addModbus();

            tabControl.setPrimaryColor(ThemeColor.PrimaryColor);
            tabControl.setSecondaryColor(ThemeColor.SecondaryColor);
            foreach (Control ctrl in GetAllControls(this))
            {
                if (ctrl.GetType() == typeof(Button))
                {
                    Button btn = (Button)ctrl;
                    btn.BackColor = ThemeColor.PrimaryColor;
                    btn.ForeColor = Color.White;
                    btn.FlatAppearance.BorderColor = ThemeColor.SecondaryColor;
                }

                if (ctrl.GetType() == typeof(GroupBox))
                {
                    GroupBox L_ctrl = (GroupBox)ctrl;
                    L_ctrl.ForeColor = ThemeColor.PrimaryColor;
                }
                if (ctrl.GetType() == typeof(Label))
                {
                    Label L_ctrl = (Label)ctrl;
                    L_ctrl.ForeColor = ThemeColor.PrimaryColor;
                }

                if (ctrl.GetType() == typeof(PiSwitch))
                {
                    PiSwitch L_ctrl = (PiSwitch)ctrl;
                    L_ctrl.L_OpenColor = ThemeColor.PrimaryColor;
                }
            }
            tabControl.Invalidate();

            this.flag = true;
            asc.controllInitializeSize(this);//初始化
        }

        private void ConfigTcpClientUi_SaveCurrConfigClickedEvent(object sender, string objectName, string ip, int port, bool isAutoConnect, bool isConnected)
        {
            // 调用自定义方法，并传递参数
           // Console.WriteLine("按钮点击事件：" + objectName + " " + ip + " " + port + " " + isConnected + " " + isAutoConnect);
           PModelControlHelp.Instance().ChenageTcpClientConfig(objectName, ip, port.ToString(),isAutoConnect);

        }

        private void ConfigTcpClientUi_stateConnectChangedEvent(object sender, string objectName, string ip, int port, bool isAutoConnect, bool isConnected)
        {
            ConfigTcpClientUi configTcpClientUi = sender as ConfigTcpClientUi;
            if (isConnected)
            {
                PTcpClientControl.Instance.GetTcpClient(objectName).CloseConnection();
                configTcpClientUi.setStateOpen(PTcpClientControl.Instance.GetTcpClient(objectName).IsRunConnect);
            }
            else {
       
                PTcpClientControl.Instance.GetTcpClient(objectName).Ip = ip;
                PTcpClientControl.Instance.GetTcpClient(objectName).Port = port;
                bool state = PTcpClientControl.Instance.GetTcpClient(objectName).Connect();
                Console.WriteLine("连接状态"+ state);
                configTcpClientUi.setStateOpen(state);
            }
        }

        private IEnumerable<Control> GetAllControls(Control control)
        {
            foreach (Control child in control.Controls)
            {
                // 返回当前子控件
                yield return child;
                // 递归返回子控件的子控件
                foreach (Control descendant in GetAllControls(child))
                {
                    yield return descendant;
                }
            }
        }
        private void SettingView_Load(object sender, EventArgs e)
        {
            tabControl.Parent = tableLayoutPanel1;
            tabControl.Dock = DockStyle.Fill;
            tabControl.setPrimaryColor(ThemeColor.PrimaryColor);
            tabControl.setSecondaryColor(ThemeColor.SecondaryColor);
            tabControl.Show();
        }

        public bool addModbus() {
            if (PModbusTCP.Instance.getPort() == 0) {
            
                return false;
            
            }

            TabPage tabPage1 = new TabPage("Modbus");
            tabPage1.Font = new System.Drawing.Font("微软雅黑", 11, System.Drawing.FontStyle.Bold);
            ModbusUi modbusUi = new ModbusUi();
            modbusUi.Show();
            modbusUi.LoadConfig();
            modbusUi.Size = new Size(1800, 1400);
            modbusUi.Dock = DockStyle.Left;
            tabPage1.Controls.Add(modbusUi);
            this.tabControl.TabPages.Add(tabPage1);
            this.tabPages.Add("Modbus", tabPage1);
            return true;

        }
        public bool addTcpClien(TcpClientInfoConfig tcpClientInfoConfig) {

            string addobjectName = tcpClientInfoConfig.ObjectName;
            //PTcpClientControl.Instance.loadTcpClient(tcpClientInfoConfig);

            bool isConnect = PTcpClientControl.Instance.GetTcpClient(tcpClientInfoConfig.ObjectName).IsRunConnect;
            ConfigTcpClientUi configTcpClientUi = new ConfigTcpClientUi(
                tcpClientInfoConfig.ObjectName, 
                tcpClientInfoConfig.Ip,
                tcpClientInfoConfig.Port,
                isConnect,
                tcpClientInfoConfig.isRunConnect
                );
            configTcpClientUi.ButtonSaveCurrConfigClickedEventHandlerClicked += ConfigTcpClientUi_SaveCurrConfigClickedEvent;
            configTcpClientUi.stateConnectEventHandlerHandlerClicked += ConfigTcpClientUi_stateConnectChangedEvent;
            TabPage tabPage1 = new TabPage(tcpClientInfoConfig.ObjectName);
            tabPage1.Font = new System.Drawing.Font("微软雅黑", 11, System.Drawing.FontStyle.Bold);
            tabPage1.Controls.Add(configTcpClientUi);
        
            configTcpClientUi.Dock = DockStyle.Fill;
            tabControl.TabPages.Add(tabPage1);
            tabPages.Add(tcpClientInfoConfig.ObjectName, tabPage1);
            return true;
        }

        private void my_SizeChanged(object sender, EventArgs e)
        {
            if (!flag) return;

            asc.controlAutoSize(this);//缩放方法
        }

        private void buttonAddDrive_Click(object sender, EventArgs e)
        {
            addDriveSelect addDriveSelectform = new addDriveSelect();
            if (addDriveSelectform.ShowDialog(this) == DialogResult.OK)
            {
                Console.WriteLine("用户选择了：" + addDriveSelectform.AddObjectName);
                if (addDriveSelectform.AddObjectName == "TCP客户端")
                {
                    TcpClientInfoConfig tcpClientInfoConfig = new TcpClientInfoConfig();
                    tcpClientInfoConfig.Ip = addDriveSelectform.ip;
                    tcpClientInfoConfig.Port = addDriveSelectform.port;
                    tcpClientInfoConfig.ObjectName = addDriveSelectform.ObjectName;
                    tcpClientInfoConfig.isRunConnect = addDriveSelectform.isRunOpen;
                    addTcpClien(tcpClientInfoConfig); 
                }
                LoadTheme();
            }
            else
            {
                Console.WriteLine("用户点击了取消或关闭窗口。");
            }

        }
    }
}
